package com.auth.secruity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Date;

/**
 * Created by Administrator on 2017/7/21 0021.
 */
public class JwtUser implements UserDetails {
    private  final String id; //ID
    private final String username;//用户名
    private final String password;//密码
    private final String email;//邮箱
    private final Collection<? extends GrantedAuthority > authorities;//权限
    private final Date lastPasswordResetDate;//上次密码重置日期

    /**
     * 生成公共构造方法
     * @param id
     * @param username
     * @param password
     * @param email
     * @param authorities
     * @param lastPasswordResetDate
     */
    public JwtUser(String id, String username, String password, String email, Collection<? extends GrantedAuthority> authorities, Date lastPasswordResetDate) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
        this.authorities = authorities;
        this.lastPasswordResetDate = lastPasswordResetDate;
    }

    /**
     * 返回分配给用户的角色集合
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @JsonIgnore
    public String getId() {
        return id;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return password;
    }

    @JsonIgnore
    @Override
    public String getUsername() {
        return username;
    }

    //账号是否未过期
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    //账号是否未锁定
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    //密码是否未过期
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    //账号是否激活
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }

    // 这个是自定义的，返回上次密码重置日期
    @JsonIgnore
    public Date getLastPasswordResetDate() {
        return lastPasswordResetDate;
    }
}
